package com.lins.framework.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MenuItem;

import com.lins.framework.R;
import com.lins.framework.common.base.BaseActivity;
import com.lins.framework.common.base.MyFragmentPagerAdapter;
import com.lins.framework.ui.cart.CartFragment;
import com.lins.framework.ui.category.CategoryFragment;
import com.lins.framework.ui.home.HomeFragment;
import com.lins.framework.ui.user.UserFragment;
import com.lins.framework.utils.BottomNavigationViewHelper;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.id_vp_content)
    ViewPager idVpContent;
    @BindView(R.id.id_bottom_navigation_view)
    BottomNavigationView idBottomNavigationView;
    private MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //默认 >3 的选中效果会影响ViewPager的滑动切换时的效果，故利用反射去掉
        BottomNavigationViewHelper.disableShiftMode(idBottomNavigationView);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {
        idBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.id_nav_home:
                        idVpContent.setCurrentItem(0);
                        break;
                    case R.id.id_nav_category:
                        idVpContent.setCurrentItem(1);
                        break;
                    case R.id.id_nav_cart:
                        idVpContent.setCurrentItem(2);
                        break;
                    case R.id.id_nav_user:
                        idVpContent.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void initLoad() {
        ArrayList<Fragment> mFragmentList = new ArrayList<>();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new CategoryFragment());
        mFragmentList.add(new CartFragment());
        mFragmentList.add(new UserFragment());
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList);
        idVpContent.setAdapter(adapter);
        // 设置ViewPager最大缓存的页面个数(cpu消耗少)
        idVpContent.setOffscreenPageLimit(2);
        idVpContent.addOnPageChangeListener(this);
        idVpContent.setCurrentItem(0);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (menuItem != null) {
            menuItem.setChecked(false);
        } else {
            idBottomNavigationView.getMenu().getItem(0).setChecked(false);
        }
        menuItem = idBottomNavigationView.getMenu().getItem(position);
        menuItem.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
